var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var md5 = require("../../utils/md5.js");
const Upyun = require('../../utils/upyun-wxapp-sdk');
var phone = '';
var wxAuthFail = true;
var inviteuid = '';

Page({
  data: {
      util: util,
      minHeight: 0,
      userInfo:{},
      sex:["请选择性别","男","女"],

      imgWH: 0,

      bottom_nav_data: [],
      isIphone_x: false,
      nav_bgColor: '#ffffff',
  },
  loadMore:function(){
      if(!noMore){
        page ++;
        this.load();
      }
  },
  onLoad: function(data){
    inviteuid = data.inviteuid ? data.inviteuid : ''; 
    this.getLoginInfo(inviteuid);
    var that = this;
    var uid = data.uid;
    // wx.showToast({
    //   title: '加载中',
    //   icon: 'loading',
    //   duration: 3000
    // })

    var tc = app.get_tc();//简体繁体转换
    that.setData({
      tc: tc
    })
    if (util.orgid == 15844){
      var titlename = tc == 1 ? '報名' : '报名';
    }
    else{
      var titlename = tc == 1 ? '留言' : '留言';
    }
    wx.setNavigationBarTitle({
      title: titlename,
    }); 

    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color,
      })
      that.resetNavColor();
    });

    //初始化底部导航
    var b_nav_data = app.getBottomNavData('liuyan/liuyan', function (data) {
      that.setData({
        bottom_nav_data: data,
        nav_bgColor: data[0].bgcolor
      });
    });

    this.setData({
      ownerUid: uid
    })

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          minHeight: res.windowHeight - 45,
          imgWH: (res.windowWidth - 18) / 4 - 12
        });
      }
    });

  },

  getLoginInfo: function (inviteuid) {
    var that = this;
    app.LoginUserInfo(function (res) {
      console.log("mine login:", res);
      wxAuthFail = res == -1;//用户拒绝授权
      if (wxAuthFail) {
        return;
      }
      that.setData({
        uid: res.uid,
        token: res.token
      });

      var id = inviteuid ? inviteuid : res.uid;
      util.api({
        data: {
          "uid": id,
          "publickey": util.publiccode,
          "ac": "userinfo"
        },
        success: function (responseData) {
          console.log("用户信息", responseData.data);
          that.setData({
            userinfo: responseData.data
          });
        }
      });

    });
  },



  //重新读取 登录用户信息
  reGetUserInfo: function (e) {
    var that = this;
    console.log("reGetUserInfo: ", e);
    if (e == 1) {
      that.getLoginInfo();
    }
  },
  goAtherPage: function (e) {
    app.saveFormId(e.detail.formId);
    var url = e.currentTarget.dataset.link;
    wx.navigateTo({ url: url })
  },
  goAtherPageRedirect: function (e) {
    var url = e.currentTarget.dataset.link;
    wx.redirectTo({ url: url })
  },


  //保存formId
  submit: function (e) {
    // app.saveFormId(e.detail.formId);
    console.log(e);
    var uname = e.detail.value.name;
    var phone = e.detail.value.moble;
    var message = e.detail.value.desc;

    var that = this;
    var privatekey = utilMd5.hexMD5(that.data.uid + 'postliuyan' + that.data.token);
    util.api({
      method: "POST",
      data: {
        ac: "postliuyan",
        uid: that.data.uid,
        uname: uname,
        phone: phone,
        message: message,
        orgid:util.orgid,
        privatekey: privatekey
      },
      success: function (responseData) {
        console.log(responseData);
        if(responseData.data.res == true){
          var contentname = this.data.tc == 1 ? '提交成功' : '提交成功';
          app.dialogToast(contentname, "success", function () {
            // wx.reLaunch({ url: '/pages/liuyan-center/liuyan-center' });
          })
        }else{
          wx.showToast({
            title: '提交失败',
            icon: 'success'
          })
        }
         
      }
    });
  },

  submitFormId: function (e) {
    // app.saveFormId(e.detail.formId);
    app.saveFormId(e.detail.formId);

    var path = e.detail.value.nav_path;
    if (path) {
      wx.redirectTo({ url: path })
    }
  },

  resetNavColor: function () {
    var bg_color = '#d8202d';
    var fg_color = '#ffffff';
    if (this.data.colorSet) {
      var colorJO = this.data.colorSet;
      bg_color = colorJO.titlebg;
      fg_color = colorJO.titlefont;
    }
    wx.setNavigationBarColor({
      frontColor: fg_color,
      backgroundColor: bg_color
    })
  },

})